/**
 * Copyright (C), 2015-2021, XXX有限公司
 * FileName: MaterialServiceImpl
 * Author:   Administrator
 * Date:     2021/8/3 0003 20:56
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package cn.hxlc.tie.demo.modular.material.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hxlc.tie.demo.core.enums.StatusEnum;
import cn.hxlc.tie.demo.core.util.NumberUtil;
import cn.hxlc.tie.demo.modular.material.entity.Material;
import cn.hxlc.tie.demo.modular.material.mapper.MaterialMapper;
import cn.hxlc.tie.demo.modular.material.model.param.MaterialParam;
import cn.hxlc.tie.demo.modular.material.model.result.MaterialResult;
import cn.hxlc.tie.demo.modular.material.service.MaterialService;
import cn.hxlc.tie.libs.mp.page.PageFactory;
import cn.hxlc.tie.libs.scanner.annotation.GetResource;
import cn.hxlc.tie.model.exp.ServiceException;
import cn.hxlc.tie.model.page.PageResult;
import cn.hxlc.tie.model.web.response.ResponseData;
import cn.hxlc.tie.system.api.constants.DistTypeConstants;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * 〈一句话功能简述〉<br> 
 * 〈〉
 *
 * @author Administrator
 * @create 2021/8/3 0003
 * @since 1.0.0
 */
@Service
@Transactional
public class MaterialServiceImpl extends ServiceImpl<MaterialMapper, Material> implements MaterialService {

    @Autowired
    private MaterialService materialService;


//    @Override
//    public MaterialResult findBySpec(MaterialParam param){
//        return null;
//    }

//    @Override
////    public List<MaterialResult> findListBySpec(MaterialParam param){
////        return this.baseMapper.customList(param);
////    }

    @Override
    public PageResult<MaterialResult> findPageBySpec(MaterialParam param) {
        Page pageContext = getPageContext();
        //System.out.println(param.getFsimplename()+"1et7yqweyqhidiahdsja");
        IPage<MaterialResult> page = this.baseMapper.customPageList(pageContext, param);
        return new PageResult<>(page);
    }


    private Page getPageContext() {

        return PageFactory.defaultPage();
    }

    public MaterialResult detail(Long fid) {
        MaterialResult result = new MaterialResult();
//        Material info = this.getById(fid);
//        BeanUtil.copyProperties(info, result);

        MaterialParam param = new MaterialParam();
        param.setFid(fid);
        List<MaterialResult> list = this.baseMapper.customList(param);
        result = list.get(0);

        return result;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(MaterialParam param) {

        NumberUtil.checkRepeatNumber(this.baseMapper, param.getFnumber(), param.getFid());
        Material entity = getEntity(param);

        if (entity.getFcreatetime() == null) {
            entity.setFcreatetime(new Date());
        }
        entity.setFupdatetime(new Date());
        this.save(entity);
    }

    private Material getEntity(MaterialParam param) {
        Material entity = new Material();
        BeanUtil.copyProperties(param, entity);
        return entity;
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchChangeState(Object[] fid, StatusEnum toEnum) {
        Material info ;
        for(int i=0;i<fid.length;i++){
            info = this.getById(new Long(fid[i].toString()));
            if(info.getFstatus()!=null && toEnum!=null && !info.getFstatus().equals(toEnum)){
                if(toEnum.getCode()==2&&info.getFstatus().getCode()==0){
                    throw new ServiceException(500,"存在启用状态，删除失败");
                }
                else if(toEnum.getCode()==2&&info.getFstatus().getCode()==1)
                {
                    info.setFnumber(info.getFnumber()+"-delete");
                    info.setFstatus(toEnum);
                    info.setFupdatetime(new Date());
                    this.updateById(info);
                }

                info.setFstatus(toEnum);
                this.updateById(info);

            }
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(MaterialParam param){
        NumberUtil.checkRepeatNumber(this.baseMapper,param.getFnumber(),param.getFid());
        Material oldEntity = getOldEntity(param);
        Material newEntity = getEntity(param);
        BeanUtil.copyProperties(newEntity, oldEntity);
        newEntity.setFupdatetime(new Date());

        this.updateById(newEntity);
    }

    private Material getOldEntity(MaterialParam param) {
        return this.getById(getKey(param));
    }

    private Serializable getKey(MaterialParam param){
        return param.getFid();
    }
}
